package com.itheima.exception.handler;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * <p>
 * 异常抛出顺序:Mapper -> Service -> Controller -> 全局异常处理器
 */
@RestControllerAdvice //@RestControllerAdvice = @ControllerAdvice + @ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result haha(Exception ex) {
        log.error("==========================");
        ex.printStackTrace(); //打印异常堆栈信息
        System.out.println(ex.getMessage()); //获取异常信息并打印
        log.error("==========================");

        //肯定出现异常了，就返回一个result对象
        return Result.error("出问题了,程序员小哥哥正在快马加鞭修改问题...");
    }
}
